package com.zhenyi.core;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;

/**
 * 处理vue数据
 * 
 * @author Frank Cen at home2019年1月20日 下午10:33:42
 *
 */
public class VueUtils {
	private static String unicode(String source) {
		StringBuffer sb = new StringBuffer();
		char[] source_char = source.toCharArray();
		String unicode = null;
		for (int i = 0; i < source_char.length; i++) {
			unicode = Integer.toHexString(source_char[i]);
			if (unicode.length() <= 2) {
				unicode = "00" + unicode;
			} else if (unicode.length() <= 3) {
				unicode = "000" + unicode;
			}
			sb.append("\\u" + unicode);
		}
		// System.out.println(sb);
		return sb.toString();
	}

	public static String string2Unicode(String string) {
		if (StrKit.isBlank(string))
			return null;
		string = string.replace("`", unicode("`"));
		string = string.replace("\"", unicode("\""));
		string = string.replace("'", unicode("'"));
		string = string.replace("/", unicode("/"));
		// string = string.replace("\n", unicode("\n"));
		// string = string.replace("n", unicode("n"));
		// string = string.replace("\\", unicode("\\"));
		return string;
	}

	public static String getVueData(Object object) {
		try {
			String json = JsonKit.toJson(object);
			json = string2Unicode(json);
			return json;
		} catch (Exception e) {
			return "";
		}
	}

}
